using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;

namespace BLT {
    public class InformationOverlay {

        #region All the Fields and their getters/setters
        /// <summary>
        /// Names and strings that represent info (level, special attributes, etc)
        /// </summary>
        private List<KeyValuePair<string, string>> information;
        public List<KeyValuePair<string, string>> m_information
        {
            get { return information; }
            set { information = value; }
        }

        /// <summary>
        /// Where it will be drawn (absolute coords. for upper left corner)
        /// </summary>
        private Vector2 location;
        public Vector2 m_location {
            get { return location; }
            set { location = value; }
        }

        /// <summary>
        /// Color to tint the image or set opacity
        /// </summary>
        private Color imageColor = Color.White;
        public Color m_imageColor
        {
            get { return imageColor; }
            set { imageColor = value; }
        }
        public byte m_imageOpacity
        {
            get { return imageColor.A; }
            set { imageColor = new Color(imageColor.R, imageColor.G, imageColor.B, value); }
        }
        /// <summary>
        /// Color to tint the font or set opacity of font
        /// </summary>
        private Color fontColor = Color.Black;
        public Color m_fontColor
        {
            get { return fontColor; }
            set { fontColor = value; }
        }
        public byte m_fontOpacity
        {
            get { return fontColor.A; }
            set { fontColor = new Color(fontColor.R, fontColor.G, fontColor.B, value); }
        }

        /// <summary>
        /// x and y size of the info. overlay
        /// </summary>
        private Vector2 size;
        public Vector2 m_size {
            get { return size; }
            set { size = value; }
        }

        /// <summary>
        /// Image to be drawn (as a background for the info. overlay)
        /// </summary>
        private Texture2D image;
        public Texture2D m_image {
            get { return image; }
            set { image = value; }
        }

        /// <summary>
        /// Font to use when drawing the text
        /// </summary>
        private SpriteFont font = null;
        public SpriteFont m_font
        {
            get { return font; }
            set {  font = value; }
        }
        #endregion

        #region All the Constructors
        public InformationOverlay(Vector2 location, Vector2 size)
        {
            this.information = new List<KeyValuePair<string, string>>();
            this.location = location;
            this.size = size;
        }

        public InformationOverlay(Vector2 location, Vector2 size, SpriteFont font)
            : this(location,size){

            this.font = font;
        }

        public InformationOverlay(Vector2 location, Vector2 size, Texture2D image, Color imageColor)
            : this(location,size)
        {
            this.image = image;
            this.imageColor = imageColor;
        }
        public InformationOverlay(Vector2 location, Vector2 size, List<KeyValuePair<string, string>> information) 
            : this(location,size)
        {
            this.information = information;
        }
        public InformationOverlay(Vector2 location, Vector2 size, Texture2D image, Color imageColor, List<KeyValuePair<string, string>> information)
            : this(location,size,image,imageColor)
        {
            this.information = information;
        }
        #endregion

        #region All other Functions
        /// <summary>
        /// Draws the information overlay
        /// </summary>
        public void Draw(SpriteBatch spriteBatch) {
            if (image != null)
            {
                spriteBatch.Draw(image, location, imageColor);
            }
            if (information.Count == 0)
            {
                return;
            }
            foreach (KeyValuePair<string, string> info in information)
            {
                spriteBatch.DrawString(font, info.Value, location, fontColor);
            }
        }

        /// <summary>
        /// Modifies a piece of information that is stored in the information overlay
        /// </summary>
        /// <param name="name">The name of the piece of information to modify</param>
        /// <param name="information">The new value for that piece of information</param>
        /// <returns>true if successful, false if the piece of information was not in this information overlay</returns>
        public bool modifyInformation(string name, string information) {
            removeInformation(name);
            addInformation(name, information);
            return true;
        }

        /// <summary>
        /// Adds a piece of information to the information overlay
        /// </summary>
        /// <param name="name">The name for the new piece of information</param>
        /// <param name="information">The new piece of information</param>
        /// <returns>true if successful, false if the given name already exists</returns>
        public bool addInformation(string name, string information) {
            this.information.Add(new KeyValuePair<string, string>(name, information));
            return true;
        }

        /// <summary>
        /// Adds a piece of information to the information overlay
        /// </summary>
        /// <param name="name">The name for the new piece of information</param>
        /// <param name="information">The new piece of information</param>
        /// <returns>true if successful, false if the given name already exists</returns>
        public string getInformation(string name)
        {
            foreach (KeyValuePair<string, string> info in this.information)
            {
                if (name.Equals(info.Key))
                {
                    return info.Value;
                }
            }
            return null;
        }

        /// <summary>
        /// Removes the piece of information of the given name from the information overlay
        /// </summary>
        /// <param name="name">The name of the piece of information to remove</param>
        public void removeInformation(string name) {
            String info_text = "";
            foreach (KeyValuePair<string, string> info in information){
                if(info.Key.Equals(name)){
                    info_text = info.Value;
                    break;
                }
            }
            information.Remove(new KeyValuePair<string,string>(name,info_text));
        }
        #endregion
    }
}